package views
{
	
	import com.oosterwijk.irc.FlexIrcClient;
	import com.oosterwijk.irc.event.*;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.StyleSheet;
	import flash.text.TextFieldAutoSize;
	import flash.ui.Keyboard;
	
	import qnx.ui.buttons.LabelButton;
	import qnx.ui.core.*;
	import qnx.ui.listClasses.ScrollDirection;
	import qnx.ui.listClasses.ScrollPane;
	import qnx.ui.text.Label;
	import qnx.ui.text.TextInput;
	
	public class PrivateChatView extends BaseView implements IRCView
	{
		private var scrlr:ScrollPane;
		private var statusText:Label;
		private var _sender:String = '';
		private var _flexIrcClient:FlexIrcClient = null;
		private var _sendBT:LabelButton;
		private var _inputTI:TextInput;
		
		public function PrivateChatView(client:FlexIrcClient, sender:String, firstMsg:String = null)
		{
			super();
			_flexIrcClient = client;
			_sender = sender;
			configUI();
			this.appendHTML("<span class='channelMsg'>*** Now talking with " + sender + "</span><br>");
			if(firstMsg != null)
				this.appendHTML(sender + ">" + firstMsg + "<br>");
		}
		
		private function configUI():void
		{
			var mainContainer:Container = new Container();
			//mainContainer.margins = Vector.<Number>([2,2,2,2]);
			mainContainer.flow = ContainerFlow.HORIZONTAL;
			//mainContainer.debugColor = 0xFFCC00;
			this.addChild(mainContainer);
			
			scrlr = new ScrollPane();
			//scrlr.setPosition(0, 0);
			scrlr.size = 100;
			scrlr.sizeUnit = SizeUnit.PERCENT;
			scrlr.scrollDirection = ScrollDirection.VERTICAL;
			
			var css:StyleSheet = new StyleSheet();
			css.setStyle("body", {fontFamily:'Courier New', fontSize:'18'});
			css.setStyle(".channelMsg", {color:'#348017'});
			css.setStyle(".serverMsg", {color:'#2554C7'});
			
			statusText = new Label();
			statusText.autoSize = TextFieldAutoSize.LEFT;
			statusText.multiline = true;
			statusText.textField.wordWrap = true;
			statusText.width = 1020;
			statusText.textField.styleSheet = css;
			statusText.htmlText = "<body>";
			//statusText.height = 470;
			scrlr.setScrollContent(statusText);
			
			/*
			bt_close = new LabelButton();
			bt_close.label = "X";
			bt_close.x = 824;
			bt_close.y = 50;
			bt_close.width = 50;
			bt_close.addEventListener(MouseEvent.CLICK, onClose);
			*/
			
			// create subcontainer as the bottom frame //
			var bottomContainer:Container = new Container();
			bottomContainer.margins = Vector.<Number>([5,5,5,5]);            
			//bottomContainer.debugColor = 0x33FF33;
			bottomContainer.size = 10;
			bottomContainer.sizeUnit = SizeUnit.PERCENT;
			bottomContainer.flow = ContainerFlow.HORIZONTAL;
			bottomContainer.align = ContainerAlign.FAR;
			bottomContainer.containment = Containment.DOCK_BOTTOM; 
			// input and send //
			_sendBT = new LabelButton();
			_sendBT.label = "Send";
			_sendBT.size = 90;
			_sendBT.addEventListener(MouseEvent.CLICK, onSend);
			
			_inputTI = new TextInput();
			_inputTI.width = 700;
			_inputTI.height = 40;
			_inputTI.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
			
			bottomContainer.addChild(_inputTI); 
			bottomContainer.addChild(_sendBT);
			
			mainContainer.addChild(bottomContainer);
			mainContainer.addChild(scrlr);
			
			mainContainer.setSize(1024, 540);
		}
		/**
		 * The sender this PrivateChatView represents 
		 */
		public function get sender():String
		{
			return this._sender;   
		}
		public  function set sender(value:String):void
		{
			this._sender = value;
		}
		
		/**
		 * Public getter and setter for the IRC Client Connection Object
		 */
		public function get flexIrcClient():FlexIrcClient
		{
			return _flexIrcClient;  
		}
		public function set flexIrcClient(value:FlexIrcClient):void
		{
			_flexIrcClient = value;
		}
		
		/**
		 * This function will register itself for the following events on the 
		 * FlexIrcClient. 
		 *
		 */
		public function registerListeners(flexIrcClient:FlexIrcClient):void
		{
			flexIrcClient.addEventListener(FlexIrcClient.PRIVATE_MESSAGE_EVENT, onPrivateMessage);
			flexIrcClient.addEventListener(FlexIrcClient.QUIT_EVENT,onQuit);
			flexIrcClient.addEventListener(FlexIrcClient.NICK_CHANGE_EVENT, onNickChange);
			flexIrcClient.addEventListener(FlexIrcClient.DISCONNECT_EVENT, onDisconnected);
		}
		
		public function cleanup(flexIrcClient:FlexIrcClient):void
		{
			flexIrcClient.removeEventListener(FlexIrcClient.PRIVATE_MESSAGE_EVENT, onPrivateMessage);
			flexIrcClient.removeEventListener(FlexIrcClient.QUIT_EVENT,onQuit);
			flexIrcClient.removeEventListener(FlexIrcClient.NICK_CHANGE_EVENT, onNickChange);
			flexIrcClient.removeEventListener(FlexIrcClient.DISCONNECT_EVENT, onDisconnected);
			_sendBT.removeEventListener(MouseEvent.CLICK, onSend);
		}
		
		protected function onPrivateMessage( event:UserEvent):void 
		{
			if (event.sender != _sender){return;}
			this.appendHTML(event.sender + "> " + event.message + "<br>");
		}
		
		protected function onQuit(event:UserEvent):void
		{
			if(event.sender == this._sender){
				this.appendHTML("<span class='serverMsg'>" + event.sender + "(" + event.login + ") (" + event.message + ")</span><br>");
			}
		}
		
		protected function onDisconnected(event:ServerEvent):void
		{
			appendHTML("<span class='serverMsg'>Disconnected.</span><br>");
		}
		
		protected function onNickChange(event:UserEvent):void
		{
			_sender = event.sender;
			this.appendHTML("<span class='serverMsg'>" + event.sender + " is known as " + event.newNick + "</span><br>");
			
			// TODO need to notify tabBar
		}
		
		private function onSend(event:MouseEvent):void
		{
			sendInputMsg();
		}
		
		private function keyPressed(e:KeyboardEvent):void{
			if(e.keyCode == Keyboard.ENTER){
				sendInputMsg();
			}
		}
		private function sendInputMsg():void
		{
			if(_inputTI.text.length == 0){
				return;
			}
			
			if(!_flexIrcClient.isConnected())
			{
				return;
			}
			
			if(_inputTI.text.charAt(0) == '/'){
				_flexIrcClient.sendRawLine(_inputTI.text.substr(1)+"\n");
				appendHTML(_inputTI.text + "<br>");
			}else{
				_flexIrcClient.sendMessage(_sender, _inputTI.text+"\n");
				appendHTML(flexIrcClient.getNick() + ">" + _inputTI.text + "<br>");
			}
			_inputTI.text = "";
		}
		public function appendHTML(txt:String):void
		{
			statusText.htmlText += txt;
			if(statusText.textHeight > scrlr.height){
				scrlr.scrollY = statusText.textHeight;
				scrlr.update();
			}
			
		}

	}
	
}